﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Diagnostics;

namespace LogDispControl
{
    public partial class NewLogHelper : UserControl
    {
        private DateTime dtInfo;
        private DateTime dtWarn;
        private Log_Helper log_helper;
        private string _logsPath = "Logs";
        private int _tsHour = 2;

        [Category("新增")]
        [Description("显示的最大日志数量")]
        public int MaxCount
        {
            set
            {
                logContainer1.MaxCount = value;
            }
            get
            {
                return logContainer1.MaxCount;
            }
        }

        [Category("新增")]
        [Description("显示日志字体大小")]
        public float FontSize
        {
            set
            {
                logContainer1.FontSize = value;
            }
            get
            {
                return logContainer1.FontSize;
            }
        }






        [Category("新增")]
        public string LogsPath
        {
            get
            {
                return _logsPath;
            }
            set
            {
                _logsPath = value;
                log_helper = new Log_Helper(_logsPath, _tsHour);
                log_helper.eventDispProcess -= new Log_Helper.delegateDispProcess(log_helper_eventDispProcess);
                log_helper.eventDispProcess += new Log_Helper.delegateDispProcess(log_helper_eventDispProcess);
            }
        }

        //时间间隔
        public int TSHour
        {
            get
            {
                return _tsHour;
            }
            set
            {
                _tsHour = value;
            }
        }

        public Log_Helper Log_helper
        {
            get
            {
                return log_helper;
            }
        }

        public NewLogHelper()
        {
            InitializeComponent();
            if (!IsDesignMode)
            {
                string m_logsPath = Application.StartupPath + $"\\{_logsPath}";

            }
        }

        //写入TXT
        public void SaveTxt(string strName, string strData, bool bAppend)
        {
            log_helper.SaveTxt(strName, strData, bAppend);
        }

        //信息日志
        public void DispInfo(string data, MessageType messageType, bool bShow)
        {
            log_helper.DispInfo(data, messageType, bShow);
        }

        //异常日志
        public void DispWarn(string data, MessageType messageType, bool bShow)
        {
            log_helper.DispWarn(data, messageType, bShow);
        }

        private void log_helper_eventDispProcess(string msg, Color foreColor)
        {
            if (this.Parent != null)
            {
                logContainer1.DispInfo(foreColor, msg);
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {
            try
            {
                string strPath = "";
                strPath = _logsPath;
                strPath += "\\";
                strPath += DateTime.Now.ToString("yyyyMMdd");
                strPath += "\\";
                string[] arrFiles = Directory.GetFiles(strPath, "Logs_*");
                if (arrFiles.Length == 0)
                {
                    MessageBox.Show("日志文件为空！");
                    return;
                }

                foreach (var file in arrFiles)
                {
                    FileInfo fileInfo = new FileInfo(file);
                    if (fileInfo.LastWriteTime >= dtInfo)
                    {
                        dtInfo = fileInfo.LastWriteTime;
                        strPath = file;
                    }
                }
                Process.Start(strPath);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {
            try
            {
                string strPath = "";
                strPath = _logsPath;
                strPath += "\\";
                strPath += DateTime.Now.ToString("yyyyMMdd");
                strPath += "\\";
                string[] arrFiles = Directory.GetFiles(strPath, "Warns_*");
                if (arrFiles.Length == 0)
                {
                    MessageBox.Show("报警日志文件为空！");
                    return;
                }

                foreach (var file in arrFiles)
                {
                    FileInfo fileInfo = new FileInfo(file);
                    if (fileInfo.LastWriteTime >= dtWarn)
                    {
                        dtWarn = fileInfo.LastWriteTime;
                        strPath = file;
                    }
                }
                Process.Start(strPath);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        //
        private void label3_Click(object sender, EventArgs e)
        {
            string strPath = "";
            strPath = _logsPath;
            //打开数据文件夹
            System.Diagnostics.Process.Start(strPath, "ExpLore");
        }

        internal  bool IsDesignMode
        {
            get
            {
                bool returnFlag = false;
                if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
                {
                    returnFlag = true;
                }
                else if (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv")
                {
                    returnFlag = true;
                }
                return returnFlag;
            }
        }

        private LanguageEnum languageEnum = LanguageEnum.Chinese;

        [Browsable(false)]
        [Localizable(false)]
        public LanguageEnum Language
        {
            get { return languageEnum; }
            set
            {
                languageEnum = value;
                switch (languageEnum)
                {
                    case LanguageEnum.Chinese:
                        Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo("zh-CN");
                        ApplyResource();
                        break;
                    case LanguageEnum.English:
                        Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
                        ApplyResource();
                        break;
                }
            }
        }

        private void ApplyResource()
        {
            System.ComponentModel.ComponentResourceManager res = new ComponentResourceManager(typeof(NewLogHelper));
            foreach (Control ctl in Controls)
            {
                if (ctl is TableLayoutPanel)
                {
                    TableLayoutPanel g = ctl as TableLayoutPanel;
                    foreach (Control item in g.Controls)
                    {
                        res.ApplyResources(item, item.Name);
                    }
                }
            }
        }

    }
}
